<?php

namespace app\cash\controller;

use app\BaseController;
use app\stores\model\order\ReasonModel;

class Reason extends BaseController{

    public function index(){
        $type = $this->request->param('type/d',0);//1退款原因2赠送原因3整单菜单备注4改价原因5撤销赠送原因6金额减免原因
        if(!in_array($type,[1,2,3,4,5,6])) return $this->error('type不正确');
        $count = ReasonModel::where(['type'=>$type,'shop_id'=>$this->request->shopId])->count();
        if($count<1){
            $reason = $default = [];
            if($type==1){
                $default[] = '押金退款';
                $default[] = '客户买错单';
                $default[] = '订单异常';
            }
            if($type==2){
                $default[] = '客情赠送';
                $default[] = '店长赠送';
                $default[] = '好评赠送';
            }
            if($type==3){
                $default[] = '全单少辣';
                $default[] = '少糖';
                $default[] = '少盐';
            }
            if($type==4){
                $default[] = '优惠';
                $default[] = '熟人减单';
            }
            if($type==5){
                $default[] = '操作失误撤销';
                $default[] = '客户不需要';
                $default[] = '更换赠品';
            }
            if($type==6){
                $default[] = '优惠';
                $default[] = '熟人减单';
            }
            for($i=0;$i<count($default);$i++){
                $reason[$i]['content'] = $default[$i];
                $reason[$i]['shop_id'] = $this->request->shopId;
                $reason[$i]['is_default'] = 1;
                $reason[$i]['type'] = $type;
            }

            $model = new ReasonModel();
            $model->saveAll($reason);
        }
        $return = ReasonModel::list(['shop_id'=>$this->request->shopId,'type'=>$type],['add_time'=>'desc'],1,999);
        return $this->success($return);
    }
    public function add(){
        $type = $this->request->param('type/d',0);//1退款原因2赠送原因3整单菜单备注4改价原因5撤销赠送原因6金额减免原因
        $content = $this->request->param('content');
        if(!in_array($type,[1,2,3,4,5,6])) return $this->error('type不正确');
        $model = new ReasonModel();
        $model->type = $type;
        $model->content = $content;
        $model->shop_id = $this->request->shopId;
        $model->save();
        return $this->success('添加成功');
    }
    public function del(){
        $reason_id = $this->request->param('reason_id/d',0);
        if(null === $reason = ReasonModel::where(['reason_id'=>$reason_id,'is_delete'=>0,'shop_id'=>$this->request->shopId])->find()){
            return $this->error('原因不存在');
        }
        if($reason->is_default==1) return $this->error('默认原因不能删除');
        $reason->is_delete = 1;
        $reason->save();
        return $this->success('删除成功');
    }
}

